/**    
 * @{#} InviteZixunActivity.java Create on 2014年8月5日 下午9:59:18    
 *    
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import cn.skinapp.R;

import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class InviteZixunActivity extends BasicActivity {

	private EditText mEditInviteCode;

	private Button mBtnInviteZixun;

	JSONObject doctorInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_invite_zixun);

		mEditInviteCode = (EditText) findViewById(R.id.edit_invite_code);
		mBtnInviteZixun = (Button) findViewById(R.id.btn_invite_zixun);

		init();

		setNavTitle("受邀患者咨询");

		try {
			doctorInfo = new JSONObject(getIntent().getStringExtra(
					Constant.IDENTITY_KEY));
			mBtnInviteZixun.setText(String.format(
					CommonUtility.getText(mBtnInviteZixun),
					doctorInfo.getString("Name")));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void onClick(View view) {
		super.onClick(view);
		int id = view.getId();
		if (id == R.id.btn_invite_zixun) {
			final String code = CommonUtility.getText(mEditInviteCode);
			if (CommonUtility.isNull(code)) {
				CommonUtility.tip(activity, "请输入邀请码");
				return;
			}
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put(Constant.PARAM_API, "Question.InviteCode.Check");
			params.put("Invite_Code", code);
			try {
				params.put("Doctor_Uid", doctorInfo.getString("Uid"));
			} catch (JSONException e1) {
				e1.printStackTrace();
			}
			RequestTaskIntercept.requestIntercept(activity,
					Constant.URL.REQUEST, new RequestHandler(activity,
							new HandlerCallback() {

								@Override
								public void callback(Object o) {
									if (!CommonUtility.isNull(o)) {
										try {
											JSONObject object = (JSONObject) o;
											if (CommonUtility
													.response200(object)) {
												int resultCode = object
														.getInt(Constant.JSON_PARAM_RES);
												if (resultCode == 1) {
													Intent intent = new Intent(
															activity,
															Cost2DoctorActivity.class);
													intent.putExtra(
															Constant.IDENTITY_KEY_1,
															code);
													intent.putExtra(
															Constant.IDENTITY_KEY,
															doctorInfo
																	.toString());
													startActivity(intent);
												} else {
													CommonUtility.tip(activity,
															"您输入的邀请码不存在");
												}
											}
										} catch (Exception e) {
											e.printStackTrace();
										}
									}
								}
							}), params);
		}
	}
	
/*	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
